<%--
  Created by IntelliJ IDEA.
  User: 86150
  Date: 2022/10/26
  Time: 15:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<body>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>用户展示</title>
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" >
</head>
<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit" >修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<%--查询条件--%>
<form class="layui-form" action="">

    <div class="layui-inline">
        <label class="layui-form-label">题目</label>
        <div class="layui-input-inline">
            <input type="text" name="question" placeholder="请输入你要查找到内容..." autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="status"  class="layui-select">
                <option value="">请选择...</option>
                <option value="1">启用</option>
                <option value="2">禁用</option>
            </select>
        </div>

    </div>

    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="importExcel">批量导入excel数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="addStu">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="delbatch">批量删除</button>
    </div>
</script>

<div class="layui-btn-container" id="upddiv" style="display: none">
    <button type="button" class="layui-btn layui-btn-normal" id="test8">选择文件</button>
    <button type="button" class="layui-btn" id="test9">开始上传</button>
</div>

<form class="layui-form" action=""  id="saveUserForm" style="display: none"  lay-filter="updateUserFormFilter">
    <input type="hidden" name="id" id="delid">
    <form class="layui-form" action="" >
        <div class="layui-form-item">
            <label class="layui-form-label">题目编号：</label>
            <div class="layui-input-block">
            <textarea type="text" name="sdsno" onblur="checksdsno(this)" hidden style="resize:none;height: 15%" required style="resize:none;height: 15%" placeholder="请输入题目编号"
                      lay-verify="required" autocomplete="off"
                      class="layui-input" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">题目：</label>
            <div class="layui-input-block">
            <textarea type="text" name="question" style="resize:none;height: 15%" required
                      placeholder="请输入题目" lay-verify="required"
                      autocomplete="off" class="layui-input"></textarea>
                <span style="color: red"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">A选项：</label>
            <div class="layui-input-block">
            <textarea type="text" name="optionA" required style="resize:none;height: 15%" placeholder="请输入A选项"
                      lay-verify="required" autocomplete="off"
                      class="layui-input" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">B选项：</label>
            <div class="layui-input-block">
            <textarea type="text" name="optionB" required style="resize:none;height: 15%" placeholder="请输入B选项"
                      lay-verify="required" autocomplete="off"
                      class="layui-input" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">C选项：</label>
            <div class="layui-input-block">
            <textarea type="text" name="optionC" required style="resize:none;height: 15%" placeholder="请输入C选项"
                      lay-verify="required" autocomplete="off"
                      class="layui-input" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">D选项：</label>
            <div class="layui-input-block">
            <textarea type="text" name="optionD" required style="resize:none;height: 15%" placeholder="请输入D选项"
                      lay-verify="required" autocomplete="off"
                      class="layui-input" ></textarea>
            </div>
        </div>

        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSaveUser">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</form>
<table class="layui-hide" id="test" lay-filter = "test"></table>
<!-- 注意：项目正式环境请勿引用该地址 -->
<script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>

<script>
    layui.use(['table','layer','form','jquery','tree'], function(){
        var tree = layui.tree;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload
            ,element = layui.element;
        var namestatus=true;
        table.render({
            // element  元素
            elem: '#test'
            ,page:true
            ,limit:10
            ,limits:[10,20,30,40,50,60,70,80,90]
            ,toolbar: '#toolbarDemo'
            ,url:'${pageContext.request.contextPath}/querysdsAll' //  表格了里面所需要的数据就是一个list里面有很多的map、对象
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                // field 后面跟的内容  必须跟后台传过来的数据里面的map 的key保持一致
                //,{field:'id', title: 'ID', sort: true, align: 'center'}
                ,{field:'sdsno', title: '编号', sort: true, align: 'center'}
                ,{field:'question', title: '题目', sort: true, align: 'center'}
                ,{field:'optionA', title: 'A', sort: true, align: 'center'}
                ,{field:'optionB', title: 'B', sort: true, align: 'center'}
                ,{field:'optionC', title: 'C', sort: true, align: 'center'}
                ,{field:'optionD', title: 'D', sort: true, align: 'center'}
                ,{fixed:'right', title:'操作', minWidth: 125, toolbar: '#barDemo'}
            ]]
        });

        //查询按钮  模糊查询
        form.on('submit(formDemo)', function (data) {
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form)//被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            alert(JSON.stringify(data.field))
            table.reload('test', {
                // where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                //     searchUserid: data.field.userid,   //排序字段            ,
                //     searchUsername: data.field.username//排序方式
                // }
                where:data.field
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        /*$(function () {
            $.ajax({
                url:"${pageContext.request.contextPath}/querysdsAll",
                dataType:"JSON",
                success:function (ret) {
                    if(ret.code===0){
                        // alert(JSON.stringify(ret.data.list))
                        //<option value="0">写作</option>
                        var htmlStr="<option value=''>--请选择--</option>";
                        for (let i = 0; i < ret.data.list.length; i++) {
                            htmlStr+="<option value="+ret.data.list[i].rid+">"+ret.data.list[i].rname+"</option>"
                        }
                        // alert(htmlStr);
                        $(".ridSelect").html(htmlStr);
                        //重新渲染表单
                        layui.use('form',function () {
                            var form=  layui.form;
                            form.render('select');
                        })
                    }
                },
            });
        })
*/

        // 工具栏事件
        table.on('toolbar(test)', function(obj){
            var uid = obj.config.uid;
            var checkStatus = table.checkStatus(uid);
            var othis = lay(this);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(layui.util.escape(JSON.stringify(data)));
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'addStu':
                    $("#saveUserForm")[0].reset();
                    $(".pass").css("display","block");
                    layer.open({
                        type: 1,
                        area: ['500px', '400px'],
                        offset: '100px',
                        content:$("#saveUserForm"),
                    });
                    break;
                case 'importExcel':
                    layer.open({
                        type: 1,
                        area: ['500px', '400px'],
                        offset: '100px',
                        content:$("#upddiv"),
                        cancel: function(index, layero){
                            layer.close(index);
                            $("#upddiv").hide();
                            return false;
                        }
                    });
                    //选完文件后不自动上传
                    upload.render({
                        elem: '#test8'
                        ,url: '${pageContext.request.contextPath}/import' //此处配置你自己的上传接口即可
                        ,auto: false// 不自动上传
                        ,accept:"file" // 允许上传所有的文件
                        //,multiple: true
                        ,bindAction: '#test9' // 绑定上传的按钮
                        ,done: function(res){
                            if(res.data==true){
                                layer.msg("导入数据成功",{"icon":1});
                                $("#upddiv").hide();
                                layer.closeAll();
                            }
                        }
                    });
                    break;
                    case 'delbatch':
                        //判断选中状态  返回它对应的id
                        var checkStatus = table.checkStatus(obj.config.id);
                        //获取选中的数据
                        var data = checkStatus.data;
                        //alert(data.length);
                        if(data.length === 0){
                            layer.msg('请选择一行')
                        }else{
                            layer.confirm('此操作不可恢复，确定删除所选中行的数据吗？', function(index){
                                //alert(JSON.stringify(data))
                                //遍历数组data  抽取所有选中行的id  sclno
                                let idList=[];
                                let sdsnoList=[];
                                for (let i = 0; i < data.length; i++) {
                                    idList[i] = (data[i].id)
                                    sdsnoList[i]=data[i].sdsno
                                }
                                //alert("idList"+idList);
                                //alert("sclnoList"+sclnoList);
                                $.ajax({
                                    type:"post",
                                    data:{id:idList,sdsno:sdsnoList},
                                    dataType: "JSON",
                                    url:"${pageContext.request.contextPath}/delbath",
                                    traditional:true,
                                    success:function (ret){
                                        //alert(ret.data);
                                        if (ret.data == true){
                                            alert("删除成功");
                                            //关闭所有弹框
                                            layer.closeAll();
                                            //刷新页面
                                          table.reload({
                                              //重新从第一页数据 刷新开始显示
                                              page: {
                                                  curr:1
                                              }
                                          })
                                        }else{
                                            alert("该题目不能被删除");
                                            //关闭所有弹框
                                            layer.closeAll();
                                            //刷新页面
                                            table.reload('test');
                                        }
                                    }
                                })
                                return false;
                            });
                        }
                        break;
            };
        });

        //触发单元格工具事件
        //监听行工具事件

        //单删除
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('此操作不可恢复，确定删除该题目吗？', function(index){
                    $.ajax({
                        type:"post",
                        data:{id:data.id,sdsno:data.sdsno},
                        url:"${pageContext.request.contextPath}/deleteSds",
                        success:function (ret){
                            if (ret.data == true){
                                alert("删除成功")
                                //关闭所有弹框
                                layer.closeAll();
                                //刷新页面
                                //刷新页面
                                table.reload({
                                    //重新从第一页数据 刷新开始显示
                                    page: {
                                        curr:1
                                    }
                                })
                            }else{
                                alert("该题目不能被删除")
                                //关闭所有弹框
                                layer.closeAll();
                                //刷新页面
                                table.reload('test');
                            }
                        }
                    })
                });
            } else if(obj.event === 'edit'){
                $(".pass").css("display","none");
                layer.open({
                    type: 1,
                    area: ['500px', '400px'],
                    offset: '100px',
                    content:$("#saveUserForm"),
                    cancel: function(index, layero){
                        layer.close(index);
                        $("#saveUserForm").hide();
                        return false;
                    }
                });
                form.val('updateUserFormFilter',data);
            }
        });
        //提交表单
        // Layui提交表单 监听提交  data代表的是form表单里面的所有的内容
        form.on('submit(formSaveUser)', function(data){
            if(namestatus){
                $.ajax({
                    url:"${pageContext.request.contextPath}/insertSds",
                    type:"post",
                    dataType:"JSON",
                    data:data.field,//表单里所有数据
                    success:function (ret){
                        alert(ret.data);
                        if (ret.data == false){
                            //关闭所有弹框
                            layer.closeAll();
                            $("#saveUserForm").hide();
                            //刷新页面
                            table.reload('test');
                        }else{
                            alert("该题目不能被修改")
                            //关闭所有弹框
                            layer.closeAll();
                            //刷新页面
                            table.reload('test');
                        }
                    }
               })
            }
            return false;
        });
        //题目编号失焦事件
        checksdsno=function (obj){
            namestatus=true;
            $.ajax({
                url:'${pageContext.request.contextPath}/queryBySdsno/'+obj.value,
                type:"post",
                success:function(d){
                    if(d.data==true){
                        namestatus=false;
                        layer.alert("该Id已经存在！！！");
                    }
                }
            })
        }

    });
</script>
</body>
</html>
</body>
</html>
